package com.huaxin.common.message;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.NaviParaOption;
import com.baidu.mapapi.utils.OpenClientUtil;

import java.util.List;

/**
 */
public class MapHelper {

    public static boolean isInstalled(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();//获取packagemanager
        final List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
        if (pinfo != null) {
            for (PackageInfo info : pinfo) {
                if (info.packageName.equals(packageName)) {
                    return true;
                }
                //pName.add(pn);
            }
        }
        return false;
    }


    final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * GCJ-02 坐标转换成 BD-09 坐标
     */
    public static double[] toBD09(double gg_lat, double gg_lon) {
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double[] ret = new double[2];
        ret[1] = z * Math.cos(theta) + 0.0065;
        ret[0] = z * Math.sin(theta) + 0.006;
        return ret;
    }

    /**
     * BD-09 坐标转换成 GCJ-02 坐标
     */
    public static double[] fromBD09(double bd_lat, double bd_lon) {
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);

        double[] ret = new double[2];
        ret[1] = z * Math.cos(theta);
        ret[0] = z * Math.sin(theta);
        return ret;
    }

    /**
     * GCJ-02 坐标转换成 BD-09 坐标
     */
    public static LatLng toBD09(LatLng gg) {
        double x = gg.longitude, y = gg.latitude;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double[] ret = new double[2];
        ret[1] = z * Math.cos(theta) + 0.0065;
        ret[0] = z * Math.sin(theta) + 0.006;
        return new LatLng(ret[0], ret[1]);
    }

    /**
     * BD-09 坐标转换成 GCJ-02 坐标
     */
    public static LatLng fromBD09(LatLng bd) {
        double x = bd.longitude - 0.0065, y = bd.latitude - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);

        double[] ret = new double[2];
        ret[1] = z * Math.cos(theta);
        ret[0] = z * Math.sin(theta);
        return new LatLng(ret[0], ret[1]);
    }

    /**
     * 腾讯地图跳转URI
     */
    public static void startTencentNavi(Context context, LatLng startPoint, String startName, LatLng endPoint, String endName) {
        if (!isInstalled(context, "com.tencent.map")) {
            Toast.makeText(context, "没有安装腾讯地图客户端", Toast.LENGTH_SHORT).show();
            return;
        }
        startPoint = fromBD09(startPoint);//坐标转换
        endPoint = fromBD09(endPoint);//坐标转换
        StringBuffer stringBuffer = new StringBuffer("qqmap://map/routeplan?type=drive&from=")
                .append(startName)
                .append("&fromcoord=").append(startPoint.latitude).append(",").append(startPoint.longitude)
                .append("&to=").append(endName)
                .append("&tocoord=").append(endPoint.latitude).append(",").append(endPoint.longitude)
                .append("&policy=2")//0：较快捷1：无高速 2：距离
                .append("&referer=trydriver");
        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
        context.startActivity(intent);
    }

    public static void startGaoDeNavi(final Context context, LatLng endPoint, String endName) {
        if (!isInstalled(context, "com.autonavi.minimap")) {
            Toast.makeText(context, "没有安装高德地图客户端", Toast.LENGTH_SHORT).show();
            return;
        }
        endPoint = fromBD09(endPoint);//坐标转换
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse(
                        "androidamap://navi?sourceApplication="
                                + context.getPackageName()
                                + "&poiname=" + endName
                                + "&lat=" + endPoint.latitude + "&lon=" + endPoint.longitude + "&dev=0"));
        intent.setPackage("com.autonavi.minimap");
        context.startActivity(intent);
    }

    public static void startBaiduMapNavi(final Context context, LatLng startPoint, String startName, LatLng endPoint, String endName) {
        NaviParaOption para = new NaviParaOption().startPoint(startPoint).startName(startName).endPoint(endPoint).endName(endName);
        try {
            BaiduMapNavigation.openBaiduMapNavi(para, context);
        } catch (BaiduMapAppNotSupportNaviException e) {
            e.printStackTrace();
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("您尚未安装百度地图app或app版本过低，点击确认安装？");
            builder.setTitle("提示");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    OpenClientUtil.getLatestBaiduMapApp(context);
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    }

}
